package controller.user;



import domain.User;
import service.user.UserService;
import service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

public class UserLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;
    UserService userService = new UserServiceImpl();
    public UserLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String un = request.getParameter("username");
        String pw = request.getParameter("password");
        if (un == null || pw == null) {
            request.getRequestDispatcher("/admin/page/404/404.html").forward(request,response);
        } else {
            String msg = null;
            User user = userService.verifyUser(un,pw);
            if (user.getUsername() != null) {
                request.getSession().setAttribute("userloginflag", user);
                response.sendRedirect(request.getContextPath() + "/admin/index.jsp");
            } else {
                System.out.println("程序执行至此处，无对应用户名密码");
                request.setAttribute("loginmsg","error");
                response.sendRedirect(request.getContextPath() + "/admin/page/login/login.html");
            }
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
